<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>省份管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <el-button size="small" type="primary" style="margin-bottom:5px"  @click="handleAdd()">新增</el-button>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="provinceName" label="省份名称" sortable width="130">
            </el-table-column>


            <el-table-column   label="状态"  >
             <template  scope="scope">
               {{scope.row.enabled==0?"禁用":"可用"}}
                </template>
            </el-table-column>
            
            <el-table-column label="操作" width="300">
                <template scope="scope">

                    <el-button   size="small" type="warning"
                               @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button   size="small" type="primary"
                                 @click="handleDetail(scope.$index, scope.row)">城市列表</el-button>


                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    layout="total,prev, pager, next"
                    :current-page.sync="currentPage"
                    :total="total">
            </el-pagination>
        </div>

        <!--预设区-->
        <div>
              <el-dialog title="新增省份" v-model="formVisible">
                <el-form :model="info">


                    <el-form-item  label="省份名称">
                        <el-input v-model="info.provinceName" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item  label="可用">
                    </el-form-item>
                    <el-switch
                            v-model="info.enabled"
                            on-color="#13ce66"
                            off-color="#ff4949">
                    </el-switch>


                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="formVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add()">确 定</el-button>
                </div>
            </el-dialog>

        </div>
    </div>
</template>

<script>
    import { Loading } from 'element-ui';
    export default {
        data() {
            return {
                info:{
                    gameId:this.gameId,
                    provinceName:"",
                    enabled:true
                },
                total:0,
                currentPage:1,
                tableData: [],
                formVisible:false,
                gameId:"",
            }
        },
        methods: {


            handleAdd(index,row){

                let self = this;
                this.action="add";
                this.info={
                    gameId:self.gameId,
                    provinceName:"",
                    enabled:true
                }
                 this.formVisible=true;

            },
            handleDetail(index,row){
                this.$router.push({ path: '/cityManage', query: { provinceId: row.provinceId } });
            },
            handleEdit(index,row){

                let self = this;
                this.action="edit";
                this.info={
                    gameId:row.gameId,
                    provinceName:row.provinceName,
                    enabled:row.enabled,
                    provinceId:row.provinceId
                }
                this.formVisible=true;
            },


            getData(){
                let self = this;
                let params = {
                    page:self.currentPage,
                    gameId:self.gameId
                };
                 self.$req.pget(self.$api("Region")+"/province",self,function(res){
                     console.log(res);
                    self.tableData = res.data.data.content;

                    self.total = res.data.data.totalElements;

                },params,true)
            },

             add(){
                let self = this;
                self.formVisible = false;
                let data = self.info;
                if(this.action=="edit"){
                    self.$req.put(self.$api("Region")+"/province",self,function(res){
                        self.getData();
                    },data,true)
                }else{

                self.$req.post(self.$api("Region")+"/province",self,function(res){
                    self.getData();
                },data,true)
                }
            }
           



        },
        mounted:function(){
            this.gameId = this.$route.query.gameId;
            this.getData();
        }
    }
</script>